{640*480}
{320*200}

{Clase MAIN}
Program Coso;

USES Draw,Mouse,Tools,Buffer,Menu,Obj;

var
i,n: Integer;
action, taction:char;

{El estado anterior del mouse}
plicki:Integer; 
plick:Integer;
click:boolean;

{Coordenadas actuales del mouse}
x,y:Integer;
xi,yi:Integer;
key:char;
keyRead:Byte;

{Objetos}
drawObj:TDraw;
mouseObj:TMouse;
tool:TTools;
buff:TBuffer;
menuObj:TMenu;
casa:TObj;
pino:TObj;

{para lìnea}
x0,y0:Integer;
xo,yo:Integer;
fin:boolean;
numLineas:integer;
prevtool,actualTool:char;
objSelected:integer;

{Guardar coordenadas anteriores del pino y casa}
prevXp, prevYp,prevXc, prevYc:integer;

{Constantes para case}
CONST 
	{Herramientas}
	move='m';scale='e';rotate='r'; shear='s';
		
    {
	 setVideo:
	 Pasar a modo video
	}
	procedure setVideo;
	BEGIN
		ASM
			MOV AX, 13H
			INT 10H
		end;

	END;

	{
	 unsetVideo:
	 Salir de modo video
	}
	procedure unsetVideo;
	BEGIN
		ASM
			MOV AX, 03h
			INT 10H
		end;

	END;
	

		
	{Esperar una tecla}
	procedure getKey;
	BEGIN
		ASM
		MOV AH,00H
		INT 16H
		MOV [keyRead],AL
		END;
		{write(keyRead);}
	END;
		

{Main }	

begin
	{Settear cosas}
	action := ' ';
	buff.initBuffer;
	tool.setObjects(mouseObj,drawObj,bufferObj,menuObj);
	setVideo;
	mouseObj.reset;
	
	{Settear el menù}
	menuObj.setMenu(drawObj,319,199);
	menuObj.show;
	menuObj.canvas;
	mouseObj.show;
	drawObj.setFree(false);
	{default tool es pencil}
	mouseObj.getXY(x,y);
	actualTool:='m';
	
	{Guardar a memoria antes de pintar casa/pino}
   	bufferObj.getMemory;
	
	{Crear la casa y el pino }
	casa.Create(40,35);
	pino.Create(45,111);
	
	{Settear su posiciòn inicial y sus cosas}
	casa.setObj(false,40,70,60,30);
	prevXc:=70; prevYc:=60;
	casa.paint;
	
	pino.setObj(true,(pi/3),150,60,30);
	prevXp:=150; prevYp:=60;
	pino.paint;
	
	
	
	{Ciclo, mientras no se presione clic izquierdo y derecho juntos}
	WHILE mouseObj.getStatus<>3 DO
	BEGIN
			{Actualizar posiciòn del mouse y buffer del teclado}
			mouseObj.getXY(x,y);			
			mouseObj.keyPressed;
			key := mouseObj.getActualKey;
			if (action<>key) and (key<>'d') then
				begin
				action := key;
				end;
			
			{Si casa està seleccionada}
			if (mouseObj.casaSelected)then
			BEGIN
								
				mouseObj.getXY(x,y);	
													
					{Case que llama a las herramientas}
					CASE action OF
						{Herramientas}
						move:
							BEGIN
							
								fin:=false;
								
								if mouseObj.getStatus=1 then
								BEGIN
																		
									mouseObj.hide;
									casa.erase(bufferObj);
														
									
									bufferObj.getMemory;
									casa.move(x,y);									
									casa.paint;
									
									mouseObj.show;
									
								END;			
								
							END;
						
						scale:BEGIN
							
								fin:=false;
								
								if mouseObj.getStatus=1 then
								BEGIN
																		
									mouseObj.hide;
									casa.erase(bufferObj);
														
									
									bufferObj.getMemory;
									casa.scale((((x-x0)/100)+1),(((y-y0)/100))+1);
									casa.paint;
									
									mouseObj.show;
									
								END;			
								
							END;
						
						rotate:
						BEGIN
							
								fin:=false;
								
								if mouseObj.getStatus=1 then
								BEGIN
																		
									mouseObj.hide;
									casa.erase(bufferObj);
														
									
									bufferObj.getMemory;
									casa.rotate(((x-x0)*pi)/180);									
									casa.paint;
									
									mouseObj.show;
									
								END;			
								
							END;
							
						shear:
						
						BEGIN
							
								fin:=false;
								
								if mouseObj.getStatus=1 then
								BEGIN
																		
									mouseObj.hide;
									casa.erase(bufferObj);
														
									
									bufferObj.getMemory;
									{casa.shear(0,0.2);}
									casa.shear((((x-x0)/100)),(((y-y0)/100)));
																		
									casa.paint;
									
									mouseObj.show;
									
								END;
						END;
						else BEGIN END;
					END;
				
				
				
			END;
			{----------------------------------------------------------------------}
			IF (mouseObj.casaSelected=FALSE)then
			BEGIN
				
				mouseObj.getXY(x,y);	
													
					{Case que llama a las herramientas}
					CASE action OF
						{Herramientas}
						move:
							BEGIN
							
								fin:=false;
								
								if mouseObj.getStatus=1 then
								BEGIN
																		
									mouseObj.hide;
									pino.erase(bufferObj);
														
									
									bufferObj.getMemory;
									pino.move(x,y);									
									pino.paint;
									
									mouseObj.show;
									
								END;			
								
							END;
						
						scale:BEGIN
							
								fin:=false;
								
								if mouseObj.getStatus=1 then
								BEGIN
																		
									mouseObj.hide;
									casa.erase(bufferObj);
														
									
									bufferObj.getMemory;
									pino.scale((((x-x0)/100)+1),(((y-y0)/100))+1);
									pino.paint;
									
									mouseObj.show;
									
								END;			
								
							END;
						
						rotate:
						BEGIN
							
								fin:=false;
								
								if mouseObj.getStatus=1 then
								BEGIN
																		
									mouseObj.hide;
									pino.erase(bufferObj);
														
									
									bufferObj.getMemory;
									pino.rotate(((x-x0)*pi)/180);									
									pino.paint;
									
									mouseObj.show;
									
								END;			
								
							END;
							
						shear:
						
						BEGIN
							
								fin:=false;
								
								if mouseObj.getStatus=1 then
								BEGIN
																		
									mouseObj.hide;
									pino.erase(bufferObj);
														
									
									bufferObj.getMemory;
									{casa.shear(0,0.2);}
									pino.shear((((x-x0)/100)),(((y-y0)/100)));
																		
									pino.paint;
									
									mouseObj.show;
									
								END;
						END;
						else BEGIN END;
					END;
				
				
			END;
			x0:=x;
			y0:=y
	END;
	unsetVideo;
	key:=mouseObj.keyPressed;
	
end.

